<?php
/**
 * Created by PhpStorm.
 * User: Aliaksei Lazerka
 * Date: 10/13/14
 * Time: 9:37 PM
 */

namespace message;

if (!defined('NYMPH_ROOT')) {
  define('NYMPH_ROOT', dirname("../../" . __FILE__));
  require(NYMPH_ROOT . '/local/loader/kernel/classes/URLClassLoader.php');
}

/**
 * Class MessageResources is used for getting messages from files
 * @package message
 */
class MessageResources
{

  /**
   * @var \parser\PropertyParser[]
   */
  private $langParsers;

  /**
   * @var MessageResources
   */
  private static $messageResources;

  public static function getInstance()
  {
    if (!self::$messageResources) {
      self::$messageResources = new MessageResources();
    }
    return self::$messageResources;
  }

  /**
   * Check if class has been already initialized
   * @return bool
   */
  public static function hasInstance()
  {
    return self::$messageResources == null ? false : true;
  }

  /**
   * @param string[] $files
   * @param string $lang language
   */
  public function addFile($files, $lang)
  {
    $this->langParsers[$lang] = new \message\PropertyParser($files);
  }

  /**
   * Returns message from the propertyParser
   * @param string $key
   * @param $lang
   * @return string
   */
  public function getMessageByKey($key, $lang)
  {
    $tmpParser = $this->langParsers[$lang];
    $tmpParser->parseAll();
    return $tmpParser->getValueByKey($key);
  }
} 